import moment from "moment/moment";

export default {
	data() {
		return {}
	},
	computed: {
		formatPrice() {
			// return (priceStr) => priceStr ? (priceStr / 100).toFixed(2) : '-.-';
			return (priceStr, type) => {
				const priceFloat = priceStr ? (priceStr * 100 / 100).toFixed(1) : '-.-';
				const priceList = priceFloat.split('.');
				if (type === 2) {
					return `<span style="font-size:1.25em;font-weight:bold;">${priceList[0]}</span>.<span style="font-size:1em;font-weight:bold;">${priceList[1]}</span>`;
				} else if (type === 1) {
					return `<span style="text-decoration:line-through">${priceFloat}</span>`;
				} else {
					return priceFloat;
				}
			}
		},
		formatZhe() {
			return (val) => {
				const str = `${val}`;
				return `<span style="font-size:1.5em;font-weight:bold;">${str.replace(/0+$/, "")}</span><span style="font-size:1em;font-weight:bold;">折</span>`;
			}
		},
		formatDate() {
			return (dateStr, formatStr) => dateStr ?
				moment(dateStr).format(formatStr || 'YYYY-MM-DD HH:mm:ss') :
				'--'
		},
		formatPhone() {
			return (phone) => phone.slice(0, 3) + "****" + phone.slice(-4);
		},
		formatThumb() {
			return (imgStr) => imgStr && typeof imgStr === 'string' && imgStr.startsWith('http') ? imgStr :
				'/static/placeholder.png'
		},
	},
	methods: {
		toServiceAgreement() {
			const title = "服务协议";
			const url = "https://env-00jxhkl4b4fs-static.normal.cloudstatic.cn/serviceAgreement/serviceAgreement.html";
			uni.navigateTo({
				url: `/pages/minePack/webview/index?title=${title}&url=${url}`
			})
		},
		toPrivacyPolicy() {
			const title = "隐私政策";
			const url = "https://env-00jxhkl4b4fs-static.normal.cloudstatic.cn/privacyPolicy/privacyPolicy.html";
			uni.navigateTo({
				url: `/pages/minePack/webview/index?title=${title}&url=${url}`
			})
		},
		// 支付收银台
		async onPaying(orderOpt, callback) {
			console.log('准备支付！', orderOpt);
			if (orderOpt._id) {
				const payRes = await this.$uniCloudCallFunction({
					name: 'xxf-order',
					action: "payOrder",
					data: {
						uid: this.$store.state.login.uid,
						openId: this.$store.state.login.openId,
						orderId: orderOpt._id
					}
				});
				console.log('payRes:', payRes);
				const self = this;
				if (payRes) {
					uni.requestPayment({
						timeStamp: payRes.timeStamp,
						nonceStr: payRes.nonceStr,
						package: payRes.package, //统一下单接口返回的 prepay_id 格式如：prepay_id=***
						signType: payRes.signType || 'MD5',
						paySign: payRes.paySign, //签名
						success(res) {
							console.log("支付成功", res)
							self.$utils.showToast(self, {
								message: '支付成功！',
								mode: 1
							});
							// 支付成功后，送出积分
							const orderStr = `****${orderOpt._id.slice(-6) || str}`;
							self.$uniCloudCallFunction({
								name: 'xxf-credits',
								action: "addCreditRow",
								data: {
									uid: self.$store.state.login.uid,
									name: `订单[${orderStr}]获得`,
									from: `${orderStr}`,
									points: parseInt(orderOpt.pay_money)
								}
							});
						},
						fail(res) {
							console.log("支付失败", res)
							self.$utils.showToast(self, {
								message: '支付失败！',
								mode: 2
							});
						},
						complete(res) {
							console.log("支付完成", res);
							try {
								self.$uniCloudCallFunction({
									name: 'xxf-order',
									action: "checkOrderStatus",
									data: {
										uid: self.$store.state.login.uid,
										token: self.$store.state.login.token,
										openId: self.$store.state.login.openId,
										orderId: orderOpt._id
									}
								});
							} catch (e) {
								console.log("complete checkOrderStatus", e);
							}

							setTimeout(() => callback(), 2500);
						}
					})
				} else {
					self.$utils.showToast(self, {
						message: '支付失败！',
						mode: 2
					});
					setTimeout(() => callback(), 2500);
				}
			}
		},
		// 解密手机号码
		async decryptPhoneNumber(e) {
			const appid = this.$store.state.login.wxappid;
			const sessionKey = this.$store.state.login.sessionKey;
			const encryptedData = e.detail.encryptedData;
			const code = e.detail.code;
			const iv = e.detail.iv;

			const decryptRes = await this.$uniCloudCallFunction({
				name: 'xxf-wechat',
				action: "wxDecryptData",
				data: {
					code: code,
				}
			});
			console.log('解密后的手机号:', decryptRes);
			return decryptRes;
		},
	}
}